import uvicorn
import logging
from config.config_manager import get_app_config

# --- 日志配置 ---
# 在加载配置前，先用基本配置确保日志能正常输出
logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

if __name__ == "__main__":
    logger.info("正在启动 HIS适配 FastAPI 服务...")
    
    # 获取应用配置
    app_config = get_app_config()
    
    # 热加载配置优化 - 只监听必要的目录
    reload_dirs = [
        "config",  # 配置目录
        "database",  # 数据库目录
        "api",  # API目录
        "service",  # 服务目录
        "task",  # 任务目录
        "static",  # 静态文件目录
        "util",  # 工具目录
        "model",  # 模型目录
    ]
    
    # 排除不需要监听的目录和文件
    reload_excludes = [
        "logs/**/*",  # 排除整个logs目录及其子目录
        "logs/*",     # 排除logs目录下的所有文件
        "cache/*",
        "__pycache__/**/*",
        "**/__pycache__/**/*",
        "*.pyc",
        "*.pyo", 
        "*.pyd",
        ".git/**/*",
        ".vscode/**/*",
        "*.log",
        "**/*.log"
    ]

    uvicorn.run(
        "app:app",
        host=app_config["host"],
        port=app_config["port"],
        reload=app_config["hot_reload"],
        reload_dirs=reload_dirs if app_config["hot_reload"] else None,
        reload_excludes=reload_excludes if app_config["hot_reload"] else None,
        log_level=app_config["log_level"],
        access_log=True,
        use_colors=True
    )
